package com.haitaoit.chengquan.module.center.adapter;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.center.model.PostModel;
import com.haitaoit.chengquan.view.CircleImageView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * Created by Administrator on 2016/10/11.
 */
public class MyForumAdapter extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<PostModel> list;
    private String type;

    public List<PostModel> getList() {
        return list;
    }

    public void setList(List<PostModel> list) {
        this.list = list;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public MyForumAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.my_forum_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.forum_ll1:
                    case R.id.forum_ll2:
                    case R.id.forum_ll3:
                        MyForumAdapter.this.backCall.deal(R.id.forum_ll1, position);
                        break;
                    case R.id.tv_look_reason:
                        MyForumAdapter.this.backCall.deal(R.id.tv_look_reason, position);
                        break;
                }
            }
        };
        holder.forum_ll1.setOnClickListener(listener);
        holder.forum_ll2.setOnClickListener(listener);
        holder.forum_ll3.setOnClickListener(listener);
        holder.tv_look_reason.setOnClickListener(listener);

        if (type.equals("1")) {
            holder.forum_ll1.setVisibility(View.VISIBLE);
            holder.forum_ll2.setVisibility(View.GONE);
            holder.forum_ll3.setVisibility(View.GONE);

            SpannableString msp = new SpannableString(list.get(position).getClass_name() + " | " + list.get(position).getTitle());
            msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0,
                    list.get(position).getClass_name().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 粗体
            msp.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.app_color)), 0,
                    list.get(position).getClass_name().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.forum_title.setText(msp);
            holder.forum_titme.setText("发布时间：" + list.get(position).getAddtime());
            holder.visit_count.setText(list.get(position).getVisit_count());
            holder.comments_count.setText(list.get(position).getComments_count());
            if (list.get(position).getStatus().equals("1")) {// status:1待审核2已审核3未通过
                holder.forum_status.setText("待审核");
                holder.tv_look_reason.setVisibility(View.INVISIBLE);
            } else if (list.get(position).getStatus().equals("2")) {
                holder.forum_status.setText("已审核");
                holder.tv_look_reason.setVisibility(View.INVISIBLE);
            } else if (list.get(position).getStatus().equals("3")) {
                holder.forum_status.setText("未通过");
                holder.tv_look_reason.setVisibility(View.VISIBLE);
            }
        } else if (type.equals("2") || type.equals("3")) {
            holder.forum_ll2.setVisibility(View.VISIBLE);
            holder.forum_ll1.setVisibility(View.GONE);
            holder.forum_ll3.setVisibility(View.GONE);

            holder.forum_name.setText("帖子标题：" + list.get(position).getTitle());
            holder.forum_addtime.setText("评论时间：" + list.get(position).getAddtime());
            holder.forum_content.setText(list.get(position).getName() + "：" + list.get(position).getContent());
        }
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.forum_ll1)
        LinearLayout forum_ll1;
        @ViewInject(R.id.forum_ll2)
        LinearLayout forum_ll2;
        @ViewInject(R.id.forum_ll3)
        LinearLayout forum_ll3;
        @ViewInject(R.id.forum_titme)
        TextView forum_titme;
        @ViewInject(R.id.tv_look_reason)
        TextView tv_look_reason;
        @ViewInject(R.id.visit_count)
        TextView visit_count;
        @ViewInject(R.id.comments_count)
        TextView comments_count;
        @ViewInject(R.id.forum_title)
        TextView forum_title;
        @ViewInject(R.id.center_img)
        CircleImageView center_img;
        @ViewInject(R.id.forum_name)
        TextView forum_name;
        @ViewInject(R.id.forum_addtime)
        TextView forum_addtime;
        @ViewInject(R.id.forum_content)
        TextView forum_content;
        @ViewInject(R.id.forum_title2)
        TextView forum_title2;
        @ViewInject(R.id.forum_name2)
        TextView forum_name2;
        @ViewInject(R.id.forum_title1)
        TextView forum_title1;
        @ViewInject(R.id.forum_status)
        TextView forum_status;
    }
}
